package jfxtras.scene.control;


import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import javafx.scene.input.KeyCode;
import javafx.scene.input.MouseEvent;

import com.sun.javafx.scene.control.behavior.BehaviorBase;
import com.sun.javafx.scene.control.behavior.KeyBinding;

/**
 * 
 * @author Tom Eugelink
 *
 */
public class SpinnerXBehavior<T> extends BehaviorBase<SpinnerX<T>>
{
	// ==================================================================================================================
	// CONSTRUCTOR
	
	/**
	 * 
	 * @param control
	 */
	public SpinnerXBehavior(SpinnerX<T> control)
	{
		super(control);
		construct();
	}
	
	/*
	 * 
	 */
	private void construct()
	{
		
	}

	// ==================================================================================================================
	// BEHAVIOR
	
	/**
	 * 
	 */
	public void decrease()
	{
		// normally the behavior would be implemented here, but that would not allow a cycling spinner to give another spinner a nudge
		getControl().decrease();
	}
	
	/**
	 * 
	 */
	public void increase()
	{
		// normally the behavior would be implemented here, but that would not allow a cycling spinner to give another spinner a nudge
		getControl().increase();
	}

	// ==================================================================================================================
	// MOUSE EVENTS
	
	/**
	 * 
	 */
	@Override public void mousePressed(MouseEvent evt)
	{
		// get the control
		SpinnerX lControl = getControl();
		
		// if a control does not have the focus, request focus
		if (!lControl.isFocused() && lControl.isFocusTraversable()) {
			lControl.requestFocus();
		}
	}
	
	// ==================================================================================================================
	// KEY EVENTS
	// TODO: not working
	
	protected final static List<KeyBinding> KEY_BINDINGS = new ArrayList<KeyBinding>();
	static 
	{
		KEY_BINDINGS.addAll(TRAVERSAL_BINDINGS);
		KEY_BINDINGS.add( new KeyBinding(KeyCode.MINUS, "IncreasePressed") );
		KEY_BINDINGS.add( new KeyBinding(KeyCode.PLUS, "DecreasePressed") );		
	}
	
	@Override protected List<KeyBinding> createKeyBindings() 
	{		
		return KEY_BINDINGS;
	}
	
	@Override protected void callAction(String name) {
		if ("IncreasePressed".equals(name)) {
			decrease();
		} 
		else if ("DecreasePressed".equals(name)) {
			increase();
		} 
		else {
			super.callAction(name);
		}
	}
}
